package com.ruoyi.web.controller.orders;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.orders.pojo.ZxOrders;
import com.ruoyi.orders.pojo.dto.ZxOrdersDto;
import com.ruoyi.orders.service.ZxOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


/**
 * @BelongsProject: RuoYi-Vue-master
 * @BelongsPackage: com.ruoyi.web.controller.orders
 * @Author: chenjiajun
 * @CreateTime: 2023-12-18  16:16
 * @Description: TODO
 * @Email: chenjiajun56@foxmail.com
 * @Version: 1.0
 */
@EnableTransactionManagement()
@RestController
@RequestMapping("orders")
@Api(tags="菜单管理")
public class OrdersController extends BaseController {
    @Autowired
    private ZxOrdersService ordersService;

    /**
     * 根据商品名称,用户nickName,商品code,订单编号查询所有订单
      * @return
     */
    @GetMapping("list")
    @ApiOperation(value = "分页查询", notes = "参数为商品名称,多个商品名称用' '隔开")
    public TableDataInfo OrderPage(String names){
        startPage();
        List<ZxOrdersDto> list = ordersService.findListByGoodsName(names);
        return getDataTable(list);
    }

    /**
     * 根据ids 进行删除
     * @param ids
     * @return
     */
    @DeleteMapping("remove/{ids}")
    @Log(title = "删除订单", businessType = BusinessType.DELETE)
    public AjaxResult removes(@PathVariable Integer[] ids){
        try {
            List<Integer> integers = Arrays.asList(ids);
            ordersService.deleteByIds(integers);
          return   AjaxResult.success();
        }catch (Exception e){
           return AjaxResult.error(200,"faild");
        }
    }

    /**
     * 下订单
     * @param
     * @return
     */
    @PostMapping("save")
    @Transactional(rollbackFor = Exception.class)
    @Log(title = "下订单", businessType = BusinessType.INSERT)
    public AjaxResult order(@RequestBody ZxOrdersDto ordersDto){
        try {
            Boolean b = ordersService.doOrder(ordersDto);
            if(!b){
                new Exception("faild");
            }
            return   AjaxResult.success(b);
        }catch (Exception e){
            return AjaxResult.error(200,e.getMessage());
        }
    }

    /**
     *模版下载
     * @param response
     */

    @PostMapping({"/importTemplate"})
    public void ImportTemplate(HttpServletResponse response) {
        ExcelUtil<ZxOrders> excelUtil = new ExcelUtil(ZxOrders.class);
        excelUtil.importTemplateExcel(response, "四色图清单");
    }

    /**
     * 导入
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping({"importData"})
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<ZxOrders> util = new ExcelUtil(ZxOrders.class);
        List<ZxOrders> ordersList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String massage = ordersService.importData(ordersList, operName);
        return AjaxResult.success(massage);
    }

    /**
     * http://localhost:9311/pic   ()服务器文件路径
     * 文件下载.返回服务器文件路径
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("upload")
    public String ExectionUpload(@RequestParam("file")MultipartFile file, HttpServletRequest request) throws IOException {
        if (file.isEmpty()){
            return "false";
        }
        File updateFile = new File("D:/Tomcat/apache-tomcat-8.5.71/webapps/pic/");
        if (!updateFile.exists()){
            updateFile.mkdir();
        }
        String fileNameNew = UUID.randomUUID().toString();
        // 客户端文件名
        String fileName = file.getOriginalFilename();
        String fileSrc = fileNameNew+fileName.substring(fileName.lastIndexOf("."));
        fileNameNew = "D:/Tomcat/apache-tomcat-8.5.71/webapps/pic/"+fileNameNew+fileName.substring(fileName.lastIndexOf("."));
        File serveFile = new File(fileNameNew);
        file.transferTo(serveFile);
        HttpSession session = request.getSession();
        session.setAttribute("picUrl",fileNameNew.replaceAll("D:/Tomcat/apache-tomcat-8.5.71/webapps/pic",
                "http://localhost:9311/pic"));
        return fileSrc;
    }

    /**
     * 出
     * @param response
     */
    @PostMapping({"/exportDiagram"})
    public void exportDiagram(HttpServletResponse response) {
        ExcelUtil<ZxOrders> excelUtil = new ExcelUtil(ZxOrders.class);
        List<ZxOrders> list = ordersService.list();
        excelUtil.exportExcel(response,list,"四色图" );
        ExcelUtil<ZxOrders> util = new ExcelUtil(ZxOrders.class);
    }


}
